package net.jblog.business.facade.impl;

import java.util.List;
import java.util.logging.Logger;

import net.jblog.business.dao.CategoryDao;
import net.jblog.business.dao.CounterDao;
import net.jblog.business.dao.PostDao;
import net.jblog.business.facade.PostFacade;
import net.jblog.data.model.Category;
import net.jblog.data.model.Post;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class PostFacadeImpl implements PostFacade {
	@SuppressWarnings("unused")
	private Logger log = Logger.getLogger(PostFacadeImpl.class.getName());
	
	private PostDao postDao;
	private CounterDao counterDao;
	private CategoryDao categoryDao;
	
	

	@Override
	public void save(Post entity) {
		Long id = getCounterDao().nextValPost();
		Key key = KeyFactory.createKey(Post.class.getSimpleName(), id);
		entity.setId(id);
		entity.setKey(key);
		getPostDao().persist(entity);
		
	}
	
	public void update(Post entity) {
		getPostDao().merge(entity);
		
	}

	public Post findById(Long id) {
		return getPostDao().findById(id);
	}
	
	
	public List<Post> findByCriteria(int index, int number, String order, boolean ascending) {
		return getPostDao().findByCriteria(index, number, order, ascending);
	}
	
	public List<Post> findLastPost(int number) {
		return getPostDao().findLastPost(number);
	}
	
	public List<Post> findAllPosts() {
		return getPostDao().findAllPosts();
	}
	
	public List<Category> findAllCategories() {
		return getCategoryDao().findAllCategories();
	}
	
	
	public PostDao getPostDao() {
		return postDao;
	}

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public CounterDao getCounterDao() {
		return counterDao;
	}

	public void setCounterDao(CounterDao counterDao) {
		this.counterDao = counterDao;
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
}
